package com.example.znbk.znbklibrary.widget;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.animation.LinearInterpolator;

public class PleaseReadyProgress extends Drawable {
	private final static int PROGRESS_FACTOR = 360;
	
	private Paint mPaint;
	private RectF mArcRect;
	
	//当前进度条进度
	private float progress;
	//边框圆颜色
	private int outlineColor;
	//内部背景圆颜色
	private int innerColor;
	//进度条颜色
	private int ringColor;
	//进度条宽度
	private int ringWidth;

	public PleaseReadyProgress(int outlineColor, int innerColor, int ringColor, int ringWidth) {
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setFilterBitmap(true);
		mArcRect = new RectF();
		
		this.outlineColor = outlineColor;
		this.innerColor = innerColor;
		this.ringColor = ringColor;
		this.ringWidth = ringWidth;
	}

	@Override
	public void draw(Canvas canvas) {
		// get view size
		final Rect bounds = getBounds();
		
		int size = bounds.height() > bounds.width() ? bounds.width() : bounds.height();
		float outerRadius = ((size / 2) * 0.75f) * 0.937f;
		float innerRadius = ((size / 2) * 0.75f) * 0.75f;
//		float outerRadius = size / 2;
//		float innerRadius = size / 2;
		float offsetX = (bounds.width() - outerRadius * 2) / 2;
		float offsetY = (bounds.height() - outerRadius * 2) / 2;
		
		//outline circle
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(1);
		mPaint.setColor(outlineColor);
		canvas.drawCircle(bounds.centerX(), bounds.centerY(), outerRadius, mPaint);
		
		//inner circle
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setColor(innerColor);
		canvas.drawCircle(bounds.centerX(), bounds.centerY(), innerRadius, mPaint);

		//ring circle
		int halfRingWidth = ringWidth / 2;
		float arcX0 = offsetX + halfRingWidth;
		float arcY0 = offsetY + halfRingWidth;
		float arcX = offsetX + outerRadius * 2 - halfRingWidth;
		float arcY = offsetY + outerRadius * 2 - halfRingWidth;
		
		mPaint.setColor(ringColor);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(ringWidth);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mArcRect.set(arcX0, arcY0, arcX, arcY);
		canvas.drawArc(mArcRect, 270, progress, false, mPaint);

	}

	@Override
	public void setAlpha(int alpha) {
		mPaint.setAlpha(alpha);
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
	}

	@Override
	public int getOpacity() {
		return mPaint.getAlpha();
	}

	public float getProgress() {
		return progress / PROGRESS_FACTOR;
	}

	public void setProgress(float progress) {
		this.progress = progress * PROGRESS_FACTOR;
		invalidateSelf();
	}

	public Animator prepareAnimator(int pleaseReadyDuration,final ProgressListener progressListener) {
		// 进度条动画
		ObjectAnimator progressAnimator = ObjectAnimator.ofFloat(this, "progress", 0f, 1f);
		progressAnimator.setDuration(pleaseReadyDuration*1000);
		progressAnimator.setInterpolator(new LinearInterpolator());
		progressAnimator.addListener(new Animator.AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {

			}

			@Override
			public void onAnimationEnd(Animator animation) {
                   if(getProgress()==1.0f){
					   setProgress(0);
					   cancleProgressAnimation();
					   progressListener.onProgressFinish();
				   }
			}

			@Override
			public void onAnimationCancel(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {

			}
		});
		return progressAnimator;
	}

	private void cancleProgressAnimation(){
		ObjectAnimator progressAnimator = ObjectAnimator.ofFloat(this, "progress", 1f, 0f);
		progressAnimator.setDuration(0);
//		progressAnimator.setInterpolator(new LinearInterpolator());
		progressAnimator.start();
	}

	public interface ProgressListener{
		void onProgressFinish();
	}

}